home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Presentations / Presentations ’91 / DAL Files / DALtool 4⁄19 (System 6.x) / DalDemoWindows.c < prev    next >
C/C++ Source or Header  |  1991-04-18  |  11KB  |  515 lines

  1. #include <QuickDraw.h>
  2. #include <MacTypes.h>
  3. #include <WindowMgr.h>
  4. #include <TextEdit.h>
  5. #include <ControlMgr.h>
  6. #include <EventMgr.h>
  7.  
  8. #include "DalDemo.h"
  9.  
  10. extern int        gNewWindowLeft, gNewWindowTop;
  11. extern int        gLinesInFolder;
  12. extern char     dirty;
  13. extern Rect    gInRect, gInScrollBarRect, gOutRect, gOutScrollBarRect;
  14. extern CharsHandle gInText;
  15.  
  16. /*** WindowType ***/
  17. int WindowType(window)
  18. WindowPtr window;
  19. {
  20.     if (window == NIL) return(NIL_WINDOW);
  21.         
  22.     if (((WindowPeek)window)->windowKind < 0) return(DA_WINDOW);
  23.     
  24.     if (((demoPeek)window)->wType == DEMO_WINDOW) return(DEMO_WINDOW);
  25.     
  26.     return(UNKNOWN_WINDOW);
  27. }
  28.  
  29. /*** CreateWindow ***/
  30. demoPeek CreateWindow()
  31. {
  32.     Rect destRect, viewRect;
  33.     Ptr        wStore;
  34.     WindowPtr    w;
  35.     demoPeek    demoWind;
  36.     
  37.     wStore = NewPtr(sizeof(demoRecord));
  38.     
  39.     if ((w = GetNewWindow(RSRC_BASE, wStore, MOVE_TO_FRONT)) == NIL)
  40.         ErrorHandler(NO_WIND);
  41.     if (((screenBits.bounds.right - gNewWindowLeft) < DRAG_THRESH) ||
  42.         ((screenBits.bounds.bottom - gNewWindowTop) < DRAG_THRESH))
  43.         {
  44.             gNewWindowLeft = WINDOW_HOME_LEFT;
  45.             gNewWindowTop = WINDOW_HOME_TOP;
  46.         }
  47.     MoveWindow(w, gNewWindowLeft,gNewWindowTop,LEAVE_WHERE_IT_IS);
  48.     gNewWindowLeft += NEW_WINDOW_OFFSET;
  49.     gNewWindowTop += NEW_WINDOW_OFFSET;
  50.     
  51.     demoWind = (demoPeek)w;
  52.     demoWind->wType = DEMO_WINDOW;
  53.     
  54.     demoWind->vScroll = NewControl( w, &gInScrollBarRect, NIL_STR, 1, 0, 0, 0,
  55.         scrollBarProc, 0L);
  56.     demoWind->vOutScroll = NewControl( w, &gOutScrollBarRect, NIL_STR, 1, 0, 0, 0,
  57.         scrollBarProc, 0L);
  58.  
  59.     ShowWindow(w);
  60.     SetPort(w);
  61.     TextFont(4);
  62.     TextSize(9);
  63.     
  64.     viewRect = gInRect;
  65.     InsetRect(&viewRect, 2, 2);
  66.     demoWind->inputTE = TENew( &viewRect, &viewRect );
  67.     SetClikLoop(NewClikLoop, demoWind->inputTE);
  68.     TEAutoView(TRUE, demoWind->inputTE);
  69.     
  70.     viewRect = gOutRect;
  71.     InsetRect(&viewRect, 2, 2);
  72.     demoWind->outputTE = TENew( &viewRect, &viewRect );
  73.     SetClikLoop(NewClikLoop, demoWind->outputTE);
  74.     TEAutoView(TRUE, demoWind->outputTE);
  75.     
  76.     demoWind->curTE = demoWind->inputTE;
  77.     
  78.     return demoWind;
  79. }
  80.  
  81. /***NewClikLook***/
  82. pascal Boolean NewClikLoop()
  83. {
  84.     WindowPtr    window;
  85.     demoPeek    demoWind;
  86.     TEHandle    te;
  87.     Rect        tempRect;
  88.     Point        mouse;
  89.     GrafPtr        oldPort;
  90.     int            amount;
  91.     RgnHandle    oldClip;
  92.     
  93.     window = FrontWindow();
  94.     if (WindowType(window) != DEMO_WINDOW) return(FALSE);
  95.     
  96.     demoWind = (demoPeek)window;
  97.     te = demoWind->curTE;
  98.     
  99.     GetPort(&oldPort);
  100.     SetPort(window);
  101.     oldClip = NewRgn();
  102.     GetClip(oldClip);
  103.     
  104.     SetRect(&tempRect, -32767, -32767, 32767, 32767);
  105.     ClipRect(&tempRect);
  106.     
  107.     GetMouse(&mouse);
  108.     
  109.     if (te == demoWind->inputTE)
  110.     {
  111.         if (mouse.v < gInRect.top)
  112.         {
  113.             amount = 1;
  114.             CommonAction(demoWind->vScroll, &amount);
  115.             if (amount != 0)
  116.                 TEScroll(0, amount * ((*te)->lineHeight), te);
  117.         }
  118.         else if (mouse.v > gInRect.bottom)
  119.         {
  120.             amount = -1;
  121.             CommonAction(demoWind->vScroll, &amount);
  122.             if (amount != 0)
  123.                 TEScroll(0, amount * ((*te)->lineHeight), te);
  124.         }
  125.     }
  126.     else
  127.     {
  128.         if (mouse.v < gOutRect.top)
  129.         {
  130.             amount = 1;
  131.             CommonAction(demoWind->vOutScroll, &amount);
  132.             if (amount != 0)
  133.                 TEScroll(0, amount * ((*te)->lineHeight), te);
  134.         }
  135.         else if (mouse.v > gOutRect.bottom)
  136.         {
  137.             amount = -1;
  138.             CommonAction(demoWind->vOutScroll, &amount);
  139.             if (amount != 0)
  140.                 TEScroll(0, amount * ((*te)->lineHeight), te);
  141.         }
  142.     }
  143.     
  144.     SetClip(oldClip);
  145.     DisposeRgn(oldClip);
  146.     SetPort(oldPort);
  147.     return(TRUE);
  148. }
  149.     
  150. /*** AdjustScrollBar ***/
  151. AdjustScrollBar(demoWind)
  152. demoPeek demoWind;
  153. {
  154.     short    value, lines, max;
  155.     short     oldValue, oldMax;
  156.     TEPtr    te;
  157.  
  158.     if (demoWind->curTE == demoWind->inputTE)
  159.     {    
  160.         oldValue = GetCtlValue(demoWind->vScroll);
  161.         oldMax = GetCtlMax(demoWind->vScroll);
  162.     }
  163.     else
  164.     {
  165.         oldValue = GetCtlValue(demoWind->vOutScroll);
  166.         oldMax = GetCtlMax(demoWind->vOutScroll);
  167.     }
  168.     te = *(demoWind->curTE);
  169.  
  170.     lines = te->nLines;
  171.  
  172.     if (*(*te->hText + te->teLength - 1) == TE_CARRIAGE_RETURN)
  173.         ++lines;
  174.  
  175.     max = lines - ((te->viewRect.bottom - te->viewRect.top) / te->lineHeight);
  176.     if (max < 0) max = 0;
  177.  
  178.     if (demoWind->curTE == demoWind->inputTE)    
  179.         SetCtlMax(demoWind->vScroll,max);
  180.     else
  181.         SetCtlMax(demoWind->vOutScroll,max);
  182.  
  183.     te = *(demoWind->curTE);
  184.     value = (te->viewRect.top - te->destRect.top) / te->lineHeight;
  185.     if (value < 0) value = 0;
  186.     else
  187.         if (value > max) value = max;
  188.  
  189.     if (demoWind->curTE == demoWind->inputTE)
  190.     {
  191.         SetCtlValue(demoWind->vScroll, value);
  192.         TEScroll(0, (te->viewRect.top - te->destRect.top) - 
  193.             (GetCtlValue(demoWind->vScroll) * te->lineHeight), demoWind->curTE);
  194.     }
  195.     else
  196.     {
  197.         SetCtlValue(demoWind->vOutScroll, value);
  198.         TEScroll(0, (te->viewRect.top - te->destRect.top) - 
  199.             (GetCtlValue(demoWind->vOutScroll) * te->lineHeight), demoWind->curTE);
  200.     }
  201. }
  202.  
  203. /*** CommonAction***/
  204. void CommonAction(control,amount)
  205. ControlHandle control;
  206. short *amount;
  207. {
  208.     short value, max;
  209.     
  210.     value = GetCtlValue(control);
  211.     max = GetCtlMax(control);
  212.     *amount = value - *amount;
  213.     if (*amount < 0)
  214.         *amount = 0;
  215.     else if (*amount > max)
  216.         *amount = max;
  217.     SetCtlValue(control, *amount);
  218.     *amount = value - *amount;
  219. }
  220.  
  221. /*** ScrollProc ***/
  222. pascal void ScrollProc(theControl, theCode)
  223. ControlHandle    theControl;
  224. int                theCode;
  225. {
  226.     int    pageSize;
  227.     int    scrollAmt;
  228.     demoPeek    demoWind;
  229.     TEPtr    te;
  230.     
  231.     if (theCode == 0)
  232.         return ;
  233.     
  234.     demoWind = (demoPeek) (*theControl)->contrlOwner;
  235.     te = *(demoWind->curTE);
  236.     pageSize = (te->viewRect.bottom - te->viewRect.top) / te->lineHeight;
  237.             
  238.     switch (theCode) {
  239.         case inUpButton: 
  240.             scrollAmt = 1;
  241.             break;
  242.         case inDownButton: 
  243.             scrollAmt = -1;
  244.             break;
  245.         case inPageUp: 
  246.             scrollAmt = pageSize;
  247.             break;
  248.         case inPageDown: 
  249.             scrollAmt = -pageSize;
  250.             break;
  251.         }
  252.     CommonAction(theControl, &scrollAmt);
  253.     if (scrollAmt != 0) 
  254.         TEScroll(0, scrollAmt * te->lineHeight, demoWind->curTE);
  255.  
  256. }
  257.  
  258. /***DoContent***/
  259. DoContent(theWindow, theEvent)
  260. WindowPtr    theWindow;
  261. EventRecord    *theEvent;
  262. {
  263.     int                thePart;
  264.     ControlHandle     theControl;
  265.     demoPeek         demoWind;
  266.     TEPtr            te;
  267.     int                value;
  268.     Point            locmouse;
  269.     Boolean            shiftDown;
  270.     
  271.     if (WindowType(theWindow) != DEMO_WINDOW) return;
  272.     
  273.     demoWind = (demoPeek) theWindow;
  274.     locmouse = theEvent->where;
  275.     GlobalToLocal( &locmouse );
  276.     
  277.     if ((thePart = FindControl(locmouse, theWindow, &theControl)) != 0 && 
  278.         theControl != NIL) 
  279.     {
  280.         switch (thePart)
  281.         {
  282.             case inUpButton:
  283.             case inDownButton:
  284.             case inPageUp:
  285.             case inPageDown:
  286.                 value = TrackControl(theControl, locmouse, (ProcPtr) ScrollProc);
  287.                 break;
  288.             case inThumb:
  289.                 value = GetCtlValue(theControl);
  290.                 thePart = TrackControl(theControl, locmouse, NIL);
  291.                 if (thePart != 0)
  292.                 {
  293.                     value -= GetCtlValue(theControl);
  294.                     te = *(demoWind->curTE);
  295.                     if (value != 0)
  296.                         TEScroll( 0, value * te->lineHeight,demoWind->curTE);
  297.                 }
  298.                 break;
  299.         }
  300.     }
  301.     else if (PtInRect(locmouse, &gInRect))
  302.     {
  303.         if (demoWind->curTE == demoWind->inputTE)
  304.         {
  305.             shiftDown = (theEvent->modifiers & shiftKey) != 0;
  306.             TEClick(locmouse,shiftDown,demoWind->inputTE);
  307.         }
  308.         else
  309.         {
  310.             SwitchToNewArea(demoWind);
  311.             TEClick(locmouse,FALSE,demoWind->inputTE);
  312.         }
  313.     }
  314.     else if (PtInRect(locmouse, &gOutRect))
  315.     {
  316.         if (demoWind->curTE == demoWind->outputTE)
  317.         {
  318.             shiftDown = (theEvent->modifiers & shiftKey) != 0;
  319.             TEClick(locmouse,shiftDown,demoWind->outputTE);
  320.         }
  321.         else
  322.         {
  323.             SwitchToNewArea(demoWind);
  324.             TEClick(locmouse,FALSE,demoWind->outputTE);
  325.         }
  326.     }
  327. }
  328.  
  329. /***SwitchToNewArea***/
  330. void SwitchToNewArea(demoWind)
  331. demoPeek demoWind;
  332. {
  333.     if (demoWind->curTE == demoWind->inputTE)
  334.     {
  335.         TurnOffTextArea(demoWind,TE_INPUT_AREA);
  336.         TurnOnTextArea(demoWind,TE_OUTPUT_AREA);
  337. /*        HandleUpdate((WindowPtr)demoWind);*/
  338.     }
  339.     else
  340.     {
  341.         TurnOffTextArea(demoWind,TE_OUTPUT_AREA);
  342.         TurnOnTextArea(demoWind,TE_INPUT_AREA);
  343. /*        HandleUpdate((WindowPtr)demoWind);*/
  344.     }
  345. }
  346.  
  347. /***TurnOnTextArea***/
  348. void TurnOnTextArea(demoWind,whichArea)
  349. demoPeek demoWind;
  350. int whichArea;
  351. {
  352.     TEPtr    te;
  353.     
  354.     if (whichArea == TE_OUTPUT_AREA)
  355.     {
  356.         te = *demoWind->outputTE;
  357.         te->viewRect.bottom = (((te->viewRect.bottom - te->viewRect.top) / 
  358.             te->lineHeight) * te->lineHeight) + te->viewRect.top;
  359.         te->destRect.bottom = te->viewRect.bottom;
  360.         HiliteControl(demoWind->vOutScroll,0);
  361.         AdjustScrollBar(demoWind);
  362.         demoWind->curTE = demoWind->outputTE;
  363.     }
  364.     else
  365.     {
  366.         te = *demoWind->inputTE;
  367.         te->viewRect.bottom = (((te->viewRect.bottom - te->viewRect.top) / 
  368.             te->lineHeight) * te->lineHeight) + te->viewRect.top;
  369.         te->destRect.bottom = te->viewRect.bottom;
  370.         HiliteControl(demoWind->vScroll,0);
  371.         AdjustScrollBar(demoWind);
  372.         demoWind->curTE = demoWind->inputTE;
  373.     }
  374.     TEActivate(demoWind->curTE);
  375. }
  376.  
  377. /***TurnOffTextArea***/
  378. void TurnOffTextArea(demoWind, whichArea)
  379. demoPeek demoWind;
  380. int    whichArea;
  381. {
  382.     if (whichArea == TE_OUTPUT_AREA)
  383.     {
  384.         HiliteControl(demoWind->vOutScroll,255);
  385.         TEDeactivate(demoWind->outputTE);
  386.     }
  387.     else
  388.     {
  389.         HiliteControl(demoWind->vScroll,255);
  390.         TEDeactivate(demoWind->inputTE);
  391.     }
  392. }
  393.     
  394.     
  395.  
  396. /***DrawDemoWind***/
  397. void DrawDemoWind(window)
  398. WindowPtr window;
  399. {
  400.     FrameRect(&gInRect);
  401.     FrameRect(&gOutRect);
  402.     DrawControls(window);
  403.     
  404.     TextFont(geneva);
  405.     TextFace(bold);
  406.     MoveTo(gInRect.left - 40, gInRect.top + 12);
  407.     DrawString("\pInput");
  408.     MoveTo(gOutRect.left - 40, gOutRect.top + 12);
  409.     DrawString("\pOutput");
  410.     
  411.     TextFont(monaco);
  412.     TextFace(0);
  413.     TEUpdate(&window->portRect, ((demoPeek)window)->inputTE);
  414.     TEUpdate(&window->portRect, ((demoPeek)window)->outputTE);
  415. }
  416.  
  417. /***HandleCloseWindow***/
  418. HandleCloseWindow(demoWind)
  419. demoPeek    demoWind;
  420. {
  421.     DALCloseLink(demoWind);
  422.     HideWindow( (WindowPtr)demoWind );
  423.     DisposeControl(demoWind->vScroll);
  424.     DisposeControl(demoWind->vOutScroll);
  425.     TEDispose(demoWind->curTE);
  426.     CloseWindow( (WindowPtr)demoWind);
  427.     DisposPtr(demoWind);
  428. }
  429.  
  430. /*** HandleTEKey ***/
  431. HandleTEKey(c)
  432. char c;
  433. {
  434.     WindowPtr     w;
  435.     demoPeek    demoWind;
  436.     int            wType;
  437.     
  438.     w = FrontWindow();
  439.     wType = WindowType(w);
  440.     
  441.     if (wType == DEMO_WINDOW)
  442.     {
  443.         demoWind = (demoPeek) w;
  444.     
  445.         switch (c)
  446.         {
  447.             case TE_TAB_CHAR:
  448.                 SwitchToNewArea(demoWind);
  449.                 break;
  450.             case TE_ENTER_KEY:
  451.                 if (demoWind->curTE == demoWind->outputTE) break;
  452.                 TESetSelect(0,32767,demoWind->curTE);
  453.                 TECopy(demoWind->curTE);
  454.                 gInText = TEGetText(demoWind->curTE);
  455.                 DALSendExecute(demoWind);
  456.                 TEDelete(demoWind->curTE);
  457.                 AdjustScrollBar(demoWind);
  458.                 break;
  459.             default:
  460.                 TEKey(c, demoWind->curTE) ;
  461.                 AdjustScrollBar(demoWind);
  462.                 dirty = 1;
  463.                 break;
  464.         }
  465.     }
  466. }
  467.         
  468.  
  469. /*** HandleActivate ***/        
  470. HandleActivate(w,f)
  471. WindowPtr w;
  472. Boolean f;
  473. {
  474.     demoPeek     demoWind;
  475.     int            wType;
  476.     Rect         theRect;
  477.     
  478.     wType = WindowType(w);
  479.     if (wType == DEMO_WINDOW )
  480.     {
  481.         demoWind = (demoPeek)w;
  482.         if (f)
  483.         {
  484.             SetPort(demoWind);
  485.             if (demoWind->curTE == demoWind->inputTE)
  486.             {
  487.                 TurnOnTextArea(demoWind,TE_INPUT_AREA);
  488.                 ShowControl(demoWind->vScroll);
  489.             }
  490.             else
  491.             {
  492.                 TurnOnTextArea(demoWind,TE_OUTPUT_AREA);
  493.                 ShowControl(demoWind->vOutScroll);
  494.             }
  495.             TEFromScrap();
  496.         }
  497.         else
  498.         {
  499.             if (demoWind->curTE == demoWind->inputTE)
  500.             {
  501.                 TurnOffTextArea(demoWind,TE_INPUT_AREA);
  502.                 HideControl(demoWind->vScroll);
  503.             }
  504.             else
  505.             {
  506.                 TurnOffTextArea(demoWind,TE_OUTPUT_AREA);
  507.                 HideControl(demoWind->vOutScroll);
  508.             }
  509.             ZeroScrap();
  510.             TEToScrap();
  511.         }
  512.     }
  513. }
  514.  
  515.